package com.hospital.insurancedip.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.insurancedip.module.entity.BaglBasyHis;
import com.hospital.insurancedip.module.mapper.InsuranceLogsMapper;
import com.hospital.insurancedip.module.service.BaglBasyHisService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Author: Yeman
 * @Date: 2023-04-03-14:51
 * @Description:
 */
@Slf4j
public class InpSettleUp extends AbstractMessageInfo {
    private InsuranceLogsMapper insuranceLogsMapper = SpringContextHolder.getBean(InsuranceLogsMapper.class);
    private BaglBasyHisService baglBasyHisService = SpringContextHolder.getBean(BaglBasyHisService.class);

    @Override
    Tip getUpDetail(JSONObject requestVal) {
        Tip tip = new SuccessTip();
        List<String> fundPayTypeList = insuranceLogsMapper.getFundPayTypeList();
        requestVal.put("infno", "230401");
        JSONObject output = requestVal.getJSONObject("input");
        String patient_id = requestVal.getString("patient_id");
        String fixmedins_code = requestVal.getString("fixmedins_code");
        String visitId = requestVal.getString("visit_id");
        JSONArray setldetail = output.getJSONArray("setldetail");
        JSONObject setlinfo = output.getJSONObject("setlinfo");
        String medins_setl_id = setlinfo.getString("medins_setl_id");
        if (medins_setl_id.substring(0, 1).equals("H")) {
            if (!ObjectUtil.isNull(setldetail)) {
                JSONArray jsonObjectList = new JSONArray();
                for (Object object : setldetail) {
                    JSONObject jsonObject = JSON.parseObject(object.toString());
                    String fund_pay_type = jsonObject.getString("fund_pay_type");
                    boolean b = fundPayTypeList.contains(fund_pay_type);
                    if (b) {
                        String crt_payb_lmt_amt = jsonObject.getString("crt_payb_lmt_amt");
                        if (ObjectUtil.isNull(crt_payb_lmt_amt)) {
                            jsonObject.put("crt_payb_lmt_amt", 0);
                        }
                        jsonObjectList.add(jsonObject);
                    }
                }
                output.put("setldetail", jsonObjectList);
                requestVal.put("input", output);
                try {
                    //只有高新及蔡家坡才需要自己组装410100
                    if (fixmedins_code.equals("H61030200230") || fixmedins_code.equals("H61032302183")) {
                        BaglBasyHis baglBasyHis = baglBasyHisService.getOne(new QueryWrapper<BaglBasyHis>().eq("patient_id", patient_id).eq("visit_id", visitId));
                        //dip结算清单上传
                        if (ObjectUtil.isNotNull(baglBasyHis) && baglBasyHis.getInhouseFlag().equals("2")) {
                            Dip990206Up dip990206Up = new Dip990206Up();
                            dip990206Up.sendDip(patient_id, visitId, requestVal, baglBasyHis);
                        }
                    }
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
            tip.setData(requestVal);
        }
        return tip;
    }

    @Override
    Tip sendOther(JSONObject requestVal) {
        return null;
    }
}
